// pages/login/login.js
import {
  loginApi
} from '../../server/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    Username: '',
    Password: ''
  },
  onLoad(){
    wx.getStorage({
      key:'token',
      success:()=>{
       wx.switchTab({
         url: '/pages/index/index',
       })
      }
    })
  },
  login() {
    if (this.data.Username && this.data.Password) {
      wx.showLoading({
        title: '登录中...',
      })
      loginApi({
        userName: this.data.Username,
        password: this.data.Password
      }).then(res => {
        if (res.code == 200) {
          wx.hideLoading()
          wx.showToast({
            title: '登录成功',
          })
          wx.setStorage({
            key: 'token',
            data: res.data.tokenValue
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      })
    } else {
      wx.showToast({
        title: '请填写完整!',
        icon: 'none'
      })
    }
  },
  inputUsername(e) {
    this.data.Username = e.detail.value
  },
  inputPassword(e) {
    this.data.Password = e.detail.value
  }
})